/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef KEY_WATCHER
#define KEY_WATCHER


#include <stdio.h>
#include "thread.hpp"
#include "sync.hpp"
#include <string>
#include <poll.h>
#include <sstream>
enum ECommandsToKeyWatcher{
  KCommandBindKey = 1,
  KCommandTestKey = 2
};

enum EMessagesFromKeyWatcher{
  KMessageFailedToOpenKeyWatcher = 0,
  KMessageSuccesfullyOpenedKeyWatcher = 1,
  KMessageKeyBinded = 2,
  KMessageEmergencyKeyPressed = 3,
  KMessageTestKeyPressed = 4,
  KMessageAnotherKeyPressedWhenTesting = 5
};

class CKeyWatcher: private gim::CThread,
                   private gim::CSync
{
public:
  CKeyWatcher(std::string input,int writePipeInt, int readPipeInt);

  virtual ~CKeyWatcher();

  //  virtual void OnKeyPressed() = 0;

  void BindKeyToWatch();
 
  int val(char c);
  void ReadEventLine(char * data);
  bool IsRunning();
private:
  FILE *iInput;
  int iKeyToWatch;
  bool iKeyBinded;
  bool iTestKey;
  bool iThreadRunning;
  int iWritePipeInt;
  int iReadPipeInt;
  int ThreadFunction(const int aThreadNumber);
  void readEventLine(char * data);

  //************************************************************
  CKeyWatcher(const CKeyWatcher&):
    CThread(),
    CSync(1,1),
    iInput(NULL),
    iKeyToWatch(0),
    iKeyBinded(false),
    iTestKey(false),
    iThreadRunning(true),
    iWritePipeInt(),
    iReadPipeInt(){}

  CKeyWatcher &operator=(const CKeyWatcher &){ return *this;}
  //*************************************************************
};

#endif
